<div class="row subsystem-header">
    <div class="pull-left">
        <span style="font-size: 14px;">任务组定义</span>
    </div>
</div>
<div class="row subsystem-toolbar">
    <div style="height: 44px; line-height: 44px;display: inline;">
        <span style="height: 30px; line-height: 30px; margin-top: 7px;display: inline"
              class="pull-left">&nbsp;项目：</span>
        <select id="h-dispatch-group-define-domain-list" class="form-control pull-left"
                style="width: 180px;height: 24px; line-height: 24px; margin-top: 10px;padding: 0px;">
        </select>
    </div>
    <div id="h-dispatch-group-define-toolbar-list" class="pull-right">
        <button onclick="DispatchGroupDefineObj.add()" class="btn btn-info btn-sm" title="新增机构信息">
            <i class="icon-plus"> 新增</i>
        </button>
        <button onclick="DispatchGroupDefineObj.edit()" class="btn btn-info btn-sm" title="编辑机构信息">
            <span class="icon-edit"> 编辑</span>
        </button>
        <button onclick="DispatchGroupDefineObj.delete()" class="btn btn-danger btn-sm" title="删除机构信息">
            <span class="icon-trash"> 删除</span>
        </button>
    </div>
</div>
<div class="row">
    <div id="h-dispatch-group-define-table-info" class="col-sm-12 col-md-12 col-lg-12">
        <table id="h-dispatch-group-define-info-table-details"
               data-toggle="table"
               data-striped="true"
               data-page-size="30"
               data-unique-id="group_id"
               data-url="/v1/dispatch/group/define"
               data-side-pagination="client"
               data-pagination="true"
               data-page-list="[20,30, 50, 100, 200,1000]"
               data-search="false">
            <thead>
            <tr>
                <th data-field="state" data-checkbox="true"></th>
                <th data-align="center" data-formatter="DispatchGroupDefineObj.formatterindex">序号</th>
                <th data-sortable="true" data-align="center" data-field="code_number">任务组编码</th>
                <th data-sortable="true" data-field="group_desc">任务组描述</th>
                <th data-sortable="true" data-align="center" data-field="create_user">创建人</th>
                <th data-sortable="true" data-align="center" data-field="create_date">创建时间</th>
                <th data-sortable="true" data-align="center" data-field="modify_user">修改人</th>
                <th data-sortable="true" data-align="center" data-field="modify_date">修改时间</th>
                <th data-sortable="true" data-align="center" data-formatter="DispatchGroupDefineObj.formatter">
                    任务组内任务管理
                </th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<script type="text/javascript">
    var DispatchGroupDefineObj = {
        $table: $('#h-dispatch-group-define-info-table-details'),
        formatter: function (value, row, index) {
            return '<span class="h-td-btn btn-success btn-xs" onclick="DispatchGroupDefineObj.getTasks(\'' + row.group_id + '\',\'' + row.group_desc + '\')">任务列表</span>'
        },
        formatterindex: function (value, row, index) {
            return index + 1;
        },
        formatterTaskId: function (value, row, index) {
            return Hutils.getLast(value);
        },
        formatterDependency: function (value, row, index) {
            return '<span class="h-td-btn btn-danger btn-xs" onclick="DispatchGroupDefineObj.deleteDependency(\'' + row.uuid + '\')"><i class="icon-trash"> 删除</i></span>'
        },
        deleteDependency: function (uuid) {
            $.HAjaxRequest({
                url: "/v1/dispatch/group/define/group/task/dependency/delete",
                type: "POST",
                data: {uuid: uuid},
                success: function () {
                    $("#h-dispatch-group-task-dependency-table").bootstrapTable('removeByUniqueId', uuid);
                }
            })
        },
        formatterTask: function (value, row, index) {
            return '<span class="h-td-btn btn-success btn-xs" onclick="DispatchGroupDefineObj.getTaskRelation(\'' + row.jobKey + '\',\'' + row.task_desc + '\',\'' + row.group_id + '\')" title="上级任务"><i class="icon-circle-arrow-up"> 上级任务</i></span>' +
                '&nbsp;&nbsp;<span class="h-td-btn btn-warning btn-xs" onclick="DispatchGroupDefineObj.getTaskArg(\'' + row.jobKey + '\',\'' + row.task_desc + '\')" title="任务参数信息"><i class="icon-question-sign"> 参数</i></span>' +
                '&nbsp;&nbsp;<span class="h-td-btn btn-danger btn-xs" onclick="DispatchGroupDefineObj.deleteTask(\'' + row.jobKey + '\')" title="从任务组中删除任务"><i class="icon-trash"> 删除</i></span>';
        },
        deleteTask: function (id) {
            $.HAjaxRequest({
                url: "/v1/dispatch/group/define/task/delete",
                type: "post",
                data: {id: id},
                success: function () {
                    $("#h-dispatch-group-task-relation-table").bootstrapTable('refresh');
                }
            })
        },
        getTaskArg: function (id, taskDesc) {
            $.Hmodal({
                header: "任务【" + taskDesc + "】参数列表",
                height: "430px",
                body: $("#h-dispatch-group-task-argument-relation-src").html(),
                submitBtn: false,
                preprocess: function () {
                    $("#h-dispatch-group-task-argument-relation-table").bootstrapTable();
                    $.getJSON("/v1/dispatch/group/define/task/argument", {id: id}, function (data) {
                        $("#h-dispatch-group-task-argument-relation-table").bootstrapTable('load', data);
                        $("#h-dispatch-group-task-argument-relation-table").find("tbody tr").each(function (index, element) {
                            var typeId = "1";
                            switch ($(element).find("td:eq(4)").html()) {
                                case "固定参数":
                                    break;
                                case "任务参数":
                                    typeId = "2";
                                    break;
                                case "任务组参数":
                                    typeId = "3";
                                    break;
                                case "批次参数":
                                    typeId = "4";
                                    break;
                            }
                            ;
                            if (typeId == "1" || typeId == "2") {
                                var argValue = $(element).find("td:eq(2) a").attr("data-value");
                                $(element).find("td:eq(2)").html(argValue);
                            } else if (typeId == "4") {
                                $(element).find("td:eq(2)").html("---");
                            }
                        });
                    });

                    $("#h-dispatch-group-task-argument-relation-table").on("editable-save.bs.table", function (editable, field, row, oldValue, $el) {
                        if ("arg_value" == field) {
                            if (row.arg_type == "3") {
                                $.HAjaxRequest({
                                    url: "/v1/dispatch/group/define/task/argument/update",
                                    type: "post",
                                    data: {arg_value: row.arg_value, uuid: id, arg_id: row.arg_id},
                                })
                            } else {
                                $.notifyClose();
                                $.Notify({
                                    message: "任务组定义中,只能给参数类型为[任务组参数]的参数赋值",
                                    type: "warning",
                                });
                            }
                        }
                    });
                }
            })
        },
        getTaskRelation: function (id, taskDesc, groupId) {
            $.Hmodal({
                header: "JobKey【" + id + "】- 任务名【" + taskDesc + "】依赖任务管理",
                height: "430px",
                submitIcon: "icon-plus",
                submitDesc: "新增依赖",
                body: $("#h-dispatch-group-task-dependency-src").html(),
                preprocess: function () {
                    $("#h-dispatch-group-task-dependency-table").bootstrapTable({
                        height: 280,
                        url: "/v1/dispatch/group/define/task/dependency",
                        queryParams: function (params) {
                            params.id = id;
                            return params
                        }
                    });
                },
                callback: function () {
                    DispatchGroupDefineObj.addDependency(id, groupId);
                }
            });
        },
        addDependency: function (id, groupId) {
            $.Hmodal({
                header: "新增依赖任务",
                height: "430px",
                body: $("#h-dispatch-group-task-dependency-relation-src").html(),
                preprocess: function () {
                    $("#h-dispatch-group-task-dependency-relation-form").bootstrapTable({
                        height: 280,
                    });
                    $.getJSON("/v1/dispatch/group/define/group/task/current", {
                        group_id: groupId,
                        id: id
                    }, function (data) {
                        $("#h-dispatch-group-task-dependency-relation-form").bootstrapTable('load', data);
                        $("#h-dispatch-group-task-dependency-relation-form").bootstrapTable('removeByUniqueId', id);
                    });
                },
                callback: function (hmode) {
                    var domain_id = $("#h-dispatch-group-define-domain-list").val();
                    var rows = $("#h-dispatch-group-task-dependency-relation-form").bootstrapTable('getSelections');

                    var args = new Array();
                    $(rows).each(function (index, element) {
                        var e = {};
                        e.domainId = domain_id;
                        e.jobKey = id;
                        e.upJobKey = element.jobKey;
                        args.push(e)
                    });

                    $.HAjaxRequest({
                        url: "/v1/dispatch/group/define/group/task/dependency",
                        type: "POST",
                        data: {JSON: JSON.stringify(args)},
                        success: function () {
                            $(hmode).remove();
                            $.getJSON("/v1/dispatch/group/define/task/dependency", {id: id}, function (val) {
                                $("#h-dispatch-group-task-dependency-table").bootstrapTable('load', val);
                            });
                        }
                    })
                },
            })
        },
        getTasks: function (groupId, groupDesc) {
            /*<![CDATA[*/
            var name = groupDesc + "中任务配置"
            Hutils.openTab({
                url: "/v1/dispatch/groupAndTask/page",
                id: "grouptask1989",
                title: name,
                data: {groupId: groupId, groupDesc: groupDesc},
                error: function (m) {
                    $.Notify({
                        title: "温馨提示：",
                        message: "您没有被授权访问这个域。",
                        type: "info",
                    })
                }
            });
            /*]]>*/
        },
        addTask: function (groupId) {
            var domain_id = $("#h-dispatch-group-define-domain-list").val();
            $.Hmodal({
                height: "430px",
                body: $("#h-dispatch-group-task-add-src").html(),
                header: "新增任务",
                callback: function (newArg) {
                    var flag = false;
                    var task_id = $("#h-dispatch-group-task-add-form").find("select[name='task_id']").val();
                    var group_id = groupId;
                    var rows = $("#h-dispatch-task-argument-relation-table").bootstrapTable('getData');
                    var arg_list = new Array();

                    $(rows).each(function (index, element) {
                        var argType = element.arg_type;
                        var argValue = element.arg_value;
                        var argId = element.arg_id;
                        /*<![CDATA[*/
                        if (argType == "3") {
                            if (argValue == "-") {
                                $.Notify({
                                    message: "这个任务含有【任务组参数】参数类型，请填写参数值",
                                    type: "danger",
                                });
                                flag = true;
                                return false;
                            } else {
                                var e = {};
                                e.arg_id = argId;
                                e.arg_value = argValue;
                                arg_list.push(e);
                            }
                        }
                        /*]]>*/
                    });
                    if (flag) {
                        return
                    }
                    $.HAjaxRequest({
                        url: "/v1/dispatch/group/define/task/add",
                        type: "POST",
                        data: {
                            task_id: task_id,
                            domain_id: domain_id,
                            group_id: group_id,
                            arg_list: JSON.stringify(arg_list)
                        },
                        success: function () {
                            $(newArg).remove();
                            $("#h-dispatch-group-task-relation-table").bootstrapTable('refresh');
                        }
                    });
                },
                preprocess: function () {

                    $.getJSON("/v1/dispatch/task/define", {domain_id: domain_id}, function (data) {
                        var arr = new Array();
                        $(data).each(function (index, element) {
                            var e = {};
                            e.id = element.taskId;
                            e.text = element.taskDesc;
                            e.upId = "##hzwy23##";
                            arr.push(e);
                        });

                        $("#h-dispatch-group-task-add-form").find("select[name='task_id']").Hselect({
                            height: "30px",
                            data: arr,
                            onclick: function () {
                                var taskId = $("#h-dispatch-group-task-add-form").find("select[name='task_id']").val();
                                $.getJSON("/v1/dispatch/task/define/argument", {task_id: taskId}, function (rst) {
                                    $("#h-dispatch-task-argument-relation-table").bootstrapTable('load', rst);
                                    $("#h-dispatch-task-argument-relation-table").find("tbody tr").each(function (index, element) {
                                        var typeId = rst[index].arg_type;
                                        if (typeId == "1" || typeId == "2") {
                                            var argValue = $(element).find("td:eq(2) a").attr("data-value");
                                            $(element).find("td:eq(2)").html(argValue);
                                        } else if (typeId == "4") {
                                            $(element).find("td:eq(2)").html("---");
                                        } else {
                                            $(element).find("td:eq(2) a").html('<span style="color: blue">-请输入参数值-</span>');
                                        }
                                    });
                                });
                            }
                        });

                        $("#h-dispatch-task-argument-relation-table").bootstrapTable({
                            height: 200,
                        });
                    });
                },
            })
        },
        add: function () {
            $.Hmodal({
                body: $("#h-dispatch-group-define-src").html(),
                height: "360px",
                header: "新增任务组",
                callback: function (hmode) {
                    $.HAjaxRequest({
                        url: "/v1/dispatch/group/define",
                        data: $("#h-dispatch-group-define-form").serialize(),
                        type: "POST",
                        success: function (data) {
                            $.Notify({
                                message: "新增任务组信息成功",
                                type: "success",
                            });
                            $(hmode).remove();
                            DispatchGroupDefineObj.$table.bootstrapTable("refresh")
                        },
                    })
                },
                preprocess: function () {
                    var domain_id = $("#h-dispatch-group-define-domain-list").val();
                    $("#h-dispatch-group-define-form").find("input[name='domain_id']").val(domain_id)
                }
            })
        },
        edit: function () {
            var row = $("#h-dispatch-group-define-info-table-details").bootstrapTable('getSelections');
            if (row.length == 0) {
                $.Notify({
                    message: "您没有选择需要编辑的费用信息",
                    type: "info",
                })
                return
            } else if (row.length > 1) {
                $.Notify({
                    message: "您选择了多行信息,请选择一行进行编辑",
                    type: "info",
                })
                return
            }

            $.Hmodal({
                body: $("#h-dispatch-group-define-src").html(),
                height: "360px",
                header: "编辑任务组",
                callback: function (hmode) {
                    $.HAjaxRequest({
                        url: "/v1/dispatch/group/define",
                        data: $("#h-dispatch-group-define-form").serialize(),
                        type: "PUT",
                        success: function (data) {
                            $.Notify({
                                message: "修改费用信息成功",
                                type: "success",
                            });
                            $(hmode).remove();
                            DispatchGroupDefineObj.$table.bootstrapTable("refresh")
                        },
                    })
                },
                preprocess: function () {
                    var domain_id = $("#h-dispatch-group-define-domain-list").val();

                    $("#h-dispatch-group-define-form").find("input[name='domain_id']").val(domain_id);
                    $("#h-dispatch-group-define-form").find("input[name='group_id']").val(row[0].code_number).attr("readonly", "readonly");
                    $("#h-dispatch-group-define-form").find("input[name='group_desc']").val(row[0].group_desc);
                }
            })
        },
        delete: function () {
            var row = $("#h-dispatch-group-define-info-table-details").bootstrapTable('getSelections');
            if (row.length == 0) {
                $.Notify({
                    message: "您没有选择需要编辑的费用信息",
                    type: "info",
                })
                return
            }
            $.Hconfirm({
                body: "点击确定,删除任务组信息",
                callback: function () {
                    $.HAjaxRequest({
                        url: "/v1/dispatch/group/define/delete",
                        type: "POST",
                        data: {JSON: JSON.stringify(row)},
                        success: function () {
                            $.Notify({
                                message: "删除任务组信息成功",
                                type: "success",
                            });
                            $(row).each(function (index, element) {
                                DispatchGroupDefineObj.$table.bootstrapTable("removeByUniqueId", element.group_id);
                            })
                        },
                    })
                }
            })
        },
    };
    $(document).ready(function () {
        var hwindow = document.documentElement.clientHeight;

        $("#h-dispatch-group-define-table-info").height(hwindow - 130);

        $.getJSON("/v1/auth/domain/self/owner", function (data) {

            $("#h-dispatch-group-define-info-table-details").bootstrapTable({
                height: hwindow - 130,
                queryParams: function (params) {
                    params.domain_id = data.domain_id;
                    return params;
                }
            });

            var arr = new Array();
            $(data.owner_list).each(function (index, element) {
                var ijs = {};
                ijs.id = element.domain_id
                ijs.text = element.domain_desc
                ijs.upId = "####hzwy23###"
                arr.push(ijs)
            });


            $("#h-dispatch-group-define-domain-list").Hselect({
                data: arr,
                height: "24px",
                width: "180px",
                value: data.domain_id,
                onclick: function () {
                    $("#h-dispatch-group-define-info-table-details").bootstrapTable('refresh');
                },
            });
        });
    });

</script>

<script id="h-dispatch-group-define-src" type="text/html">
    <form id="h-dispatch-group-define-form">
        <div class="col-sm-12 col-md-12 col-lg-12">
            <span>任务组编码</span>
            <input name="group_id" placeholder="由1-30位字母,数字组成" class="form-control"
                   style="height: 30px; line-height: 30px; width: 100%;"/>
        </div>
        <div class="col-sm-12 col-md-12 col-lg-12" style="margin-top: 20px;">
            <span>任务组名称</span>
            <input name="group_desc" placeholder="任务组的详细描述信息" class="form-control"
                   style="height: 30px; line-height: 30px; width: 100%;"/>
        </div>
        <input name="domain_id" style="display: none;"/>
    </form>
</script>

<script id="h-dispatch-group-task-argument-relation-src" type="text/html">
    <table id="h-dispatch-group-task-argument-relation-table"
           data-toggle="table"
           data-striped="true"
           data-unique-id="taskId"
           data-height="290"
           data-side-pagination="client"
           data-pagination="false"
           data-search="false">
        <thead>
        <tr>
            <th data-align="center" data-field="code_number">参数编码</th>
            <th data-align="left" data-field="arg_desc">参数名称</th>
            <th data-align="right" data-editable="true" data-field="arg_value">参数设定值</th>
            <th data-align="center" data-field="sort_id">序号</th>
            <th data-align="center" data-field="arg_type_desc">参数类型</th>
        </tr>
        </thead>
    </table>
</script>

<script id="h-dispatch-group-task-add-src" type="text/html">
    <form id="h-dispatch-group-task-add-form">
        <div class="col-sm-12 col-md-12 col-lg-12">
            <span style="display: inline-block;">任务编码: </span>
            <select name="task_id"
                    class="form-control"
                    style="height: 30px; line-height: 30px; width: 100%;">
            </select>
        </div>
        <div class="col-sm-12 col-md-12 col-lg-12" style="margin-top: 20px;">
            <span>任务参数信息</span>
            <table id="h-dispatch-task-argument-relation-table"
                   data-toggle="table"
                   data-striped="true"
                   data-unique-id="uuid"
                   data-url="/v1/dispatch/task/define/argument"
                   data-side-pagination="client"
                   data-pagination="false"
                   data-search="false">
                <thead>
                <tr>
                    <th data-align="center" data-field="code_number">参数编码</th>
                    <th data-align="left" data-field="arg_desc">参数名称</th>
                    <th data-align="right" data-field="arg_value" data-editable="true">参数设定值</th>
                    <th data-align="center" data-field="sort_id">序号</th>
                    <th data-align="center" data-field="arg_type_desc">参数类型</th>
                </tr>
                </thead>
            </table>
        </div>
    </form>
</script>

<script id="h-dispatch-group-task-dependency-relation-src" type="text/html">
    <table id="h-dispatch-group-task-dependency-relation-form"
           data-toggle="table"
           data-striped="true"
           data-unique-id="id"
           data-side-pagination="client"
           data-pagination="false"
           data-search="false">
        <thead>
        <tr>
            <th data-field="state" data-checkbox="true"></th>
            <th data-align="center" data-width="46px" data-formatter="DispatchGroupDefineObj.formatterindex">序号</th>
            <th data-align="center" data-width="320px" data-field="jobKey">上级JobKey</th>
            <th data-align="center" data-field="task_id" data-formatter="DispatchGroupDefineObj.formatterTaskId">任务编码
            </th>
            <th data-align="left" data-field="task_desc">任务名称</th>
        </tr>
        </thead>
    </table>
</script>